package com.bos.controller;

import com.bos.dao.InstansiDAO;
import com.bos.dao.JabatanDAO;
import com.bos.dao.PengelolaDAO;
import com.bos.domain.Instansi;
import com.bos.domain.Jabatan;
import com.bos.domain.Pengelola;
import com.bos.editor.InstansiEditor;
import com.bos.editor.JabatanEditor;
import com.bos.editor.PengelolaEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class PengelolaController {

    private PengelolaDAO pengelolaDAO;
    private InstansiDAO instansiDAO;
    private JabatanDAO jabatanDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setPengelolaDAO(PengelolaDAO pengelolaDAO) {
        this.pengelolaDAO = pengelolaDAO;
    }

    @Autowired
    public void setInstansiDAO(InstansiDAO instansiDAO) {
        this.instansiDAO = instansiDAO;
    }

    @Autowired
    public void setJabatanDAO(JabatanDAO jabatanDAO) {
        this.jabatanDAO = jabatanDAO;
    }

    /* -------------------------- Other Methods -------------------------- */

    @RequestMapping(value = "/pengelola", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("data", pengelolaDAO.getAll());

        return "pengelola/index";
    }

    @RequestMapping(value = "/pengelola/{id}", method = RequestMethod.GET)
    public String show(ModelMap modelMap, @PathVariable("id") Pengelola pengelola) {
        modelMap.addAttribute("data", pengelola);

        return "pengelola/show";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Instansi.class, new InstansiEditor(instansiDAO));
        binder.registerCustomEditor(Jabatan.class, new JabatanEditor(jabatanDAO));
        binder.registerCustomEditor(Pengelola.class, new PengelolaEditor(pengelolaDAO));
    }

}
